<template>
  <el-dialog title="编辑" :visible.sync="visible" :close-on-click-modal="false"
    :before-close="handleCloseDialog" width="680px">
    <el-form :model="dataObject" :rules="rules" ref="adminForm" label-width="100px" size="small" >
      <el-form-item label="账号：" prop="phoneNo">
        <el-input v-model="dataObject.phoneNo" type="number" placeholder="请输入手机号码"  disabled></el-input>
      </el-form-item>
      <el-form-item label="昵称：" prop="nickname">
        <el-input v-model="dataObject.nickname"  placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="vip等级：" prop="levelId">
        <el-select v-model="dataObject.levelId" placeholder="请选择vip等级">
          <el-option
            v-for="item in levelOptions"
            :key="item.id"
            :label="item.nameZh"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
       <!-- <el-form-item label="状态：" prop="state">
          <el-select v-model="dataObject.state" placeholder="请选择状态">
            <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="余额：" prop="balance">
        <el-input v-model="dataObject.balance" placeholder="请输入余额" ></el-input>
      </el-form-item> -->
<!--  
      <el-form-item label="邮箱：" prop="email">
        <el-input v-model="dataObject.email"  clearable></el-input>
      </el-form-item> -->
      
      <el-form-item label="信誉度：" prop="credit">
        <el-input v-model="dataObject.credit" type="number" placeholder="请输入信誉度" clearable></el-input>
      </el-form-item>

      <el-form-item label="提现开关：" prop="dealState">
          <el-select v-model="dataObject.dealState" placeholder="请选择替提现开关">
            <el-option
              v-for="item in dealStateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleConfirmDialog('adminForm')" size="mini" :loading="loading">确 定</el-button>
      <el-button @click="handleCloseDialog" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { updateMemberInfo } from '@/api/member/index';
import { getMemberLevelList } from '@/api/member/index';
import { createNamespacedHelpers } from "vuex";
const { mapState } = createNamespacedHelpers("member");
const defaultObject = {
  id: '',
  email:'',
  nickname: "",
  phoneNo: "",
  balance: '',
  levelId:'',
  dealState:'',
  credit:''
};

export default {
  name: "deductionDialog",
  data() {
    return {
      visible:true,
      loading: false,
      dataObject: Object.assign({}, defaultObject),
      levelOptions:[]
    };
  },
  props: {
    row: {
      type: Object,
      default: {}
    }
  },
  computed: {
    ...mapState({
      rules: state => state.rules,
      stateOptions: state => state.routes.stateOptions,
      dealStateOptions: state => state.routes.dealStateOptions,
    })
  },
  mounted() {
    this.getLevelOptions();
    Object.keys(this.dataObject).forEach(key => {
       this.dataObject[key] = this.row[key];
    });
    
  },
  methods: {

    //获取用户等级
    getLevelOptions(){
       getMemberLevelList({pageNum:1,pageSize:999}).then(response => {
        this.levelOptions = response.data.list || [];
      });
    },

    handleCloseDialog() {
      this.$emit("handle-cancel");
    },
    handleConfirmDialog(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading=true;

          updateMemberInfo({...this.dataObject,id:this.row.id}).then(response => {
            this.resetForm(formName);
            this.$message({
              message: "提交成功",
              type: "success",
              duration: 1000
            });
            this.loading=false;
            this.$emit("handle-confirm"); 
          }).catch(()=>{
            this.loading=false;
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dataObject = Object.assign({}, defaultObject);
    }
  }
};
</script>
<style scoped lang="scss">

</style>